package com.system.permissiondemo.controller;

import com.system.permissiondemo.common.RequestHolder;
import com.system.permissiondemo.common.ResultDTO;
import com.system.permissiondemo.dto.DeptLevelDTO;
import com.system.permissiondemo.param.DeptParam;
import com.system.permissiondemo.service.SysDeptService;
import com.system.permissiondemo.service.SysTreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping(value = "/sys/dept")
@RestController
@Api(tags = "部门信息管理")
@Slf4j
public class DepartmentController {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysTreeService treeService;

    @ApiOperation(value = "添加部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "部门id",paramType = "query",dataType ="Integer"),
            @ApiImplicitParam(name = "name",value = "部门名称",required = true,paramType = "query",dataType = "String",defaultValue = "游戏部"),
            @ApiImplicitParam(name = "parentId",value = "上一级部门id",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "seq",value = "部门在当前层级下的顺序（从小到大）",required = true,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "remark",value = "备注",paramType = "query",dataType = "String",defaultValue = "游戏部")
    })
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResultDTO saveDept(DeptParam param){
        if (RequestHolder.getCurrentUser()==null){
            return ResultDTO.errorOfLogin();
        }
        sysDeptService.save(param);
        return ResultDTO.okOf();
    }

    @ApiOperation(value = "部门树结构")
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    public ResultDTO tree(){
        List<DeptLevelDTO> dtoList = treeService.deptTree();
        return ResultDTO.okOf(dtoList);
    }

    @ApiOperation(value = "更新/编辑部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "部门id",required = true,paramType = "query",dataType ="Integer"),
            @ApiImplicitParam(name = "name",value = "部门名称",required = true,paramType = "query",dataType = "String",defaultValue = "游戏部"),
            @ApiImplicitParam(name = "parentId",value = "上一级部门id",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "seq",value = "部门在当前层级下的顺序（从小到大）",required = true,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "remark",value = "备注",paramType = "query",dataType = "String",defaultValue = "游戏部")
    })
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ResultDTO updateDept(DeptParam param){
        if (RequestHolder.getCurrentUser()==null){
            return ResultDTO.errorOfLogin();
        }
        sysDeptService.update(param);
        return ResultDTO.okOf();
    }
}
